package com.gaotong.science.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.gaotong.science.entity.DataFile;
import com.gaotong.science.util.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/receive")
public class ReciveController {
    @Value("${fileUrl}")
    private String path;

    @PostMapping("/file")
    public R receiveFile(@RequestBody Map<String,Object> params){

        String name = (String)params.get("name");
//        List<DataFile> list =(List<DataFile>)params.get("list");
        Object list = params.get("list");
        JSONArray array = JSONUtil.parseArray(list);
        List<DataFile> listData = JSONUtil.toList(array, DataFile.class);
        String[] split = name.split("\\.");
        System.out.println(split[0]);
        String path1=path+split[0]+".csv";
        try {
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path1), "UTF-8"));
            for (int i = 0; i < listData.size(); i++) {
                DataFile dataFile = listData.get(i);
                System.out.println(dataFile);
                Integer rowIndex = dataFile.getRowIndex();
                out.write(rowIndex+"");
                out.write(",");
                String colIndex = dataFile.getColIndex();
                out.write(colIndex);
                out.write(",");
                String value = dataFile.getValue();
                out.write(value);
                out.newLine();
            }
            out.flush();
            out.close();
            String path2 = "/root/zheng/15_modify_content.py";
            String lujing="/root/zheng/outdata/";
            String[] args = new String[]{"python3.8", path2, lujing+name,path1};
            R r = Python1.runPython(args);
            return r;

        }catch (Exception e){
            e.printStackTrace();
        }
        return R.error();

    }

}
